var app = getApp()
Page({
  data: {
    searchLetter: ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
    showLetter: "",
    winHeight: 0,
    // tHeight: 0,
    // bHeight: 0,
    arrivalid: '',
    //cityList: [],
    isShowLetter: false,
    scrollTop: 0, //置顶高度
    scrollTopId: '', //置顶id
    city: "厦门",
    cityList: ''
  },
  onLoad: function(options) {
    var that = this
    // 生命周期函数--监听页面加载
    let dataArr = options.data.split(',');
    var id = dataArr[0];
    var citys = dataArr[1];
    var host = this.data.hotcityList
    var searchLetter = that.data.searchLetter;
    //var cityList = city.cityList();
    //var cityList = city.cityList();

    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var itemH = winHeight / searchLetter.length;
    var tempObj = [];
    if (id == 1) {
      this.setData({
        city: citys
      })
    } else if (id == 2) {
      this.setData({
        city: citys
      })
    } else {
      this.setData({
        city: citys
      })
    }
    for (var i = 0; i < searchLetter.length; i++) {
      var temp = {};
      temp.name = searchLetter[i];
      temp.tHeight = i * itemH;
      temp.bHeight = (i + 1) * itemH;
      tempObj.push(temp)
    }

    wx.request({
      url: app.apiUrl('c=index&a=getconfig'),
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var cityList = res.data.data.opensite
        var host = res.data.data.hot_site
        var nowlist = res.data.data.now_site
        //对城市信息进行分组
        var tempObj = [];
        for (var i = 0; i < searchLetter.length; i++) {
          var initial = searchLetter[i];
          var cityLists = [];
          var tempArr = {};
          tempArr.initial = initial;
          for (var j = 0; j < cityList.length; j++) {
            if (initial == cityList[j].initial) {
              cityLists.push(cityList[j]);
              //console.log(cityList[j])
            }
          }
          tempArr.cityList = cityLists;
          tempObj.push(tempArr);
          //console.log(tempArr)
        }
        //console.log(tempObj)
  
        //去掉为空的元素
        var result = [];
        for (var i = 0; i < tempObj.length; i++) {
          var objs = tempObj[i];
          if (tempObj[i].cityList == '') {
            //console.log(tempObj[i])
            delete tempObj[i];
            //console.log(tempObj[i])
            result.push(objs)
          }
          //console.log(tempObj[i])
        }

        that.setData({
          cityList: tempObj,
          hotcityList: host,
          searchLetter: tempObj,
          nowlist: nowlist
        })
      }
    })
    that.setData({
      winHeight: winHeight,
      itemH: itemH,
      //searchLetter: tempObj,
      //cityList: cityList,
      arrivalid: id
    })
    //console.log(searchLetter)
    //console.log(cityList)
    //console.log(city)
  },
  clickLetter: function(e) {
    console.log(e.currentTarget.dataset.letter)
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function() {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择城市
  bindCity: function(e) {
    city: '';
    var theid = this.data.arrivalid
    // var city = e.currentTarget.dataset.city;
    // app.globalData.city = city 
    var value = e.currentTarget.dataset.city; //把本页面需要传递的参数保存在本地缓存里
    var id = e.currentTarget.dataset.id
    wx.request({
      url: app.apiUrl('c=index&a=setconfig'),
      data: {
        city_id: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {}

    })
    if (theid == 1) {
      wx.setStorageSync('concent', value);
      wx.setStorageSync('cityid', id);
      wx.switchTab({
        url: 'index'
      })
    }
    this.setData({
      city: e.currentTarget.dataset.city
    })
    console.log(e.currentTarget.dataset.city)

    console.log(e)
  },
  //选择当前城市
  choosecity: function(e) {
    city: '';
    // var city = e.currentTarget.dataset.city;
    // app.globalData.city = city 
    var value = e.currentTarget.dataset.city; //把本页面需要传递的参数保存在本地缓存里
    var id = e.currentTarget.dataset.id
    wx.request({
      url: app.apiUrl('c=index&a=setconfig'),
      data: {
        city_id: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {}

    })

    wx.setStorageSync('concent', value);
    wx.setStorageSync('cityid', id);
    wx.switchTab({
      url: 'index'
    })

    this.setData({
      city: e.currentTarget.dataset.city
    })
  },
  //选择热门城市
  bindHotCity: function(e) {
    city: '';
    var url = "https://www.xtuan.com/api/weixinapp/?c=index&a=setconfig"
    var theid = this.data.arrivalid
    var id = e.currentTarget.dataset.id
    wx.request({
      url: url,
      data: {
        city_id: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {}
    })
    this.setData({
      city: e.currentTarget.dataset.city
    })
    // var city =e.currentTarget.dataset.city;
    // app.globalData.city = city 
    var value = e.currentTarget.dataset.city; //把本页面需要传递的参数保存在本地缓存里
    wx.setStorageSync('concent', value);
    wx.setStorageSync('cityid', id);
    if (theid == 1) {
      wx.switchTab({
        url: 'index'
      })
    }
  },
  //点击热门城市回到顶部
  hotCity: function() {
    this.setData({
      scrollTop: 0,
    })
  }
})